package com.cy.uv.controller;

import com.cy.uv.entity.Merchant;
import com.cy.uv.service.MerchantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "MerchantController", description = "商户管理接口")
@RestController
@RequestMapping("/api/merchants")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @Operation(summary = "根据名称获取商户列表", description = "根据商户名称查询商户列表")
    @GetMapping("/name/{name}")
    public ResponseEntity<List<Merchant>> getMerchantByName(@PathVariable String name) {
        List<Merchant> merchants = merchantService.getMerchantByName(name);
        return ResponseEntity.ok(merchants);
    }

    @Operation(summary = "根据ID获取商户详情", description = "根据商户ID获取商户详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<Merchant> getMerchantById(@PathVariable Integer id) {
        Merchant merchant = merchantService.getById(id);
        return ResponseEntity.ok(merchant);
    }

    @Operation(summary = "添加商户", description = "添加一个新的商户到数据库")
    @PostMapping("/add")
    public ResponseEntity<Merchant> addMerchant(@RequestBody Merchant merchant) {
        boolean isSaved = merchantService.save(merchant);
        return ResponseEntity.ok(isSaved ? merchant : null);
    }

    @Operation(summary = "更新商户信息", description = "根据传入的商户信息更新数据库中的记录")
    @PutMapping("/update")
    public ResponseEntity<Merchant> updateMerchant(@RequestBody Merchant merchant) {
        boolean isUpdated = merchantService.updateById(merchant);
        return ResponseEntity.ok(isUpdated ? merchant : null);
    }

    @Operation(summary = "删除商户", description = "根据商户ID删除商户，建议改为逻辑删除")
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteMerchant(@PathVariable Integer id) {
        boolean isDeleted = merchantService.removeById(id);
        return ResponseEntity.ok(isDeleted ? "Delete successful" : "Delete failed");
    }

    @Operation(summary = "获取所有商户列表", description = "返回全部商户信息，注意分页和加密处理")
    @GetMapping("/list")
    public ResponseEntity<List<Merchant>> getAllMerchant() {
        List<Merchant> merchants = merchantService.list();
        return ResponseEntity.ok(merchants);
    }

    @Operation(summary = "根据分类获取商户列表", description = "根据商户分类获取商户列表")
    @GetMapping("/sort/{sort}")
    public ResponseEntity<List<Merchant>> getMerchantBySort(@PathVariable String sort) {
        List<Merchant> merchants = merchantService.getMerchantBySort(sort);
        return ResponseEntity.ok(merchants);
    }

    @Operation(summary = "根据状态获取商户列表", description = "根据商户状态获取商户列表")
    @GetMapping("/status/{status}")
    public ResponseEntity<List<Merchant>> getMerchantByStatus(@PathVariable Integer status) {
        List<Merchant> merchants = merchantService.getMerchantByStatus(status);
        return ResponseEntity.ok(merchants);
    }

    @Operation(summary = "根据类型获取商户列表", description = "根据商户类型获取商户列表")
    @GetMapping("/type/{type}")
    public ResponseEntity<List<Merchant>> getMerchantByType(@PathVariable Integer type) {
        List<Merchant> merchants = merchantService.getMerchantByType(type);
        return ResponseEntity.ok(merchants);
    }

   /* @Operation(summary = "修改商家staus", description = "商家处于上架还是下架状态还是审核状态")
    @PutMapping("/status/{id}/{status}")
    public ResponseEntity<String> updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        boolean isUpdated = merchantService.update();
        return ResponseEntity.ok(isUpdated ? "Update successful" : "Update failed");
    }*/
    }